--[[
ClosetGnome, a World of Warcraft addon to manage item sets.
Copyright (C) 2007 Rabbit.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
]]

local ClosetGnome = ClosetGnome
assert(ClosetGnome, "ClosetGnome-UI-LDB requires ClosetGnome to run.")

if not LibStub then return end
local ldb = LibStub:GetLibrary("LibDataBroker-1.1", true)
if not ldb then return end

local al = LibStub("AceLocale-3.0")

-----------------------------------------------------------------------
-- Localization
do
	do
		local L = al:NewLocale("ClosetGnome-UI-LDB", "enUS", true)
		L["Use the menu (|cffeda55fRight-Click|r) to equip, edit or delete item sets. |cffeda55fClick|r to create a new set."] = true
		L["|cffeda55fClick|r to equip, |cffeda55fShift-Click|r to edit and |cffeda55fCtrl-Click|r to delete %s."] = true
		L["no_menu"] = "You haven't created any ClosetGnome sets, so there's no menu for you here. Look in the interface options and try to click the display to create a set."
	end
	do
		local L = al:NewLocale("ClosetGnome-UI-LDB", "frFR")
		if L then
			L["Use the menu (|cffeda55fRight-Click|r) to equip, edit or delete item sets. |cffeda55fClick|r to create a new set."] = "Utilisez le menu (|cffeda55fclic droit|r) pour équiper, éditer ou supprimer des ensembles. |cffeda55fClic gauche|r pour créer un nouvel ensemble."
			L["|cffeda55fClick|r to equip, |cffeda55fShift-Click|r to edit and |cffeda55fCtrl-Click|r to delete %s."] = "|cffeda55fClic gauche|r pour équiper, |cffeda55fShift-clic gauche|r pour éditer et |cffeda55fCtrl-clic gauche|r pour supprimer %s."
		end
	end
	do
		local L = al:NewLocale("ClosetGnome-UI-LDB", "zhCN")
		if L then
			L["Use the menu (|cffeda55fRight-Click|r) to equip, edit or delete item sets. |cffeda55fClick|r to create a new set."] = "使用菜单 (|cffeda55f右键|r)装备、编辑或删除物品设置。|cffeda55f点击|r 创建新的设置。"
			L["|cffeda55fClick|r to equip, |cffeda55fShift-Click|r to edit and |cffeda55fCtrl-Click|r to delete %s."] = "|cffeda55f点击|r装备, |cffeda55fShift+点击|r 编辑 |cffeda55fCtrl+点击|r 删除 %s。"
			L["no_menu"] = "你还没有ClosetGnome设置，所以没有菜单给你选择。看用户界面选项或点击这里创建一个设置。"
		end
	end
	do
		local L = al:NewLocale("ClosetGnome-UI-LDB", "zhTW")
		if L then
			L["Use the menu (|cffeda55fRight-Click|r) to equip, edit or delete item sets. |cffeda55fClick|r to create a new set."] = "使用功能表 (|cffeda55f右鍵|r)裝備、編輯或刪除物品設置。|cffeda55f點擊|r 創建新的設置。"
			L["|cffeda55fClick|r to equip, |cffeda55fShift-Click|r to edit and |cffeda55fCtrl-Click|r to delete %s."] = "|cffeda55f點擊|r裝備, |cffeda55fShift+點擊|r 編輯 |cffeda55fCtrl+點擊|r 刪除 %s。"
			L["no_menu"] = "你還沒有ClosetGnome設置，所以沒有功能表給你選擇。看使用者介面選項或點擊這裡創建一個設置。"
		end
	end
end

local L = al:GetLocale("ClosetGnome-UI-LDB")

-----------------------------------------------------------------------
-- FuBar UI for ClosetGnome

local defaultIcon = "Interface\\Icons\\INV_Chest_Cloth_17"

CGLDB = ldb:NewDataObject("ClosetGnome", {
	type = "data source",
	text = "ClosetGnome",
	icon = defaultIcon,
})

local callbacks = LibStub("CallbackHandler-1.0"):New(CGLDB)

local icon = LibStub("LibDBIcon-1.0", true)
local menu = {}
local menuNeedsUpdate = true

local CGLDB = CGLDB
local lastEquippedSet = nil

local function menuSorter(a, b)
	return a.text > b.text
end

local function updateMenu()
	menuNeedsUpdate = nil
	menu = wipe(menu)
	for k in pairs(ClosetGnome.db.char.set) do
		if not ClosetGnome:HideSetFromUI(k) then
			local name = ClosetGnome:GetSetDisplayName(k)
			if ClosetGnome.db.char.keybindings[k] then
				name = name .. " |cff999999(" .. ClosetGnome.db.char.keybindings[k] .. ")|r"
			end
			local x = {
				text = name,
				tooltipTitle = k,
				tooltipText = L["|cffeda55fClick|r to equip, |cffeda55fShift-Click|r to edit and |cffeda55fCtrl-Click|r to delete %s."]:format(k),
				func = function()
					if IsShiftKeyDown() then
						ClosetGnome:CreateOrUpdateSet(k)
					elseif IsControlKeyDown() then
						ClosetGnome:DeleteSet(k)
					else
						ClosetGnome:WearSet(k)
					end
				end,
				icon = ClosetGnome.db.char.icons[k],
			}
			table.insert(menu, x)
		end
	end
	table.sort(menu, menuSorter)
	local num = #menu
	callbacks:Fire("OnMenu", menu)
	if #menu > num then
		table.insert(menu, num+1, { text = "" })
	end
end

local popupFrame = CreateFrame("Frame", "ClosetGnomeMenu", UIParent, "UIDropDownMenuTemplate")
function CGLDB.OnClick(self, button)
	if button == "RightButton" then
		if menuNeedsUpdate then updateMenu() end
		if #menu == 0 then
			print(L["no_menu"])
		else
			EasyMenu(menu, popupFrame, self, 20, 4, "MENU")
		end
	else
		if IsShiftKeyDown() then
			ClosetGnome:CreateOrUpdateSet(lastEquippedSet)
		else
			ClosetGnome:CreateOrUpdateSet()
		end
	end
end

function CGLDB.OnTooltipShow(tt)
	tt:AddLine("ClosetGnome")
	tt:AddLine(L["Use the menu (|cffeda55fRight-Click|r) to equip, edit or delete item sets. |cffeda55fClick|r to create a new set."], 0.2, 1, 0.2, 1)
end

local function updateDisplay()
	CGLDB.text = lastEquippedSet and ClosetGnome:GetSetDisplayName(lastEquippedSet) or "ClosetGnome"
	CGLDB.icon = ClosetGnome.db.char.icons[lastEquippedSet] or defaultIcon
end

local function updateLatest(event, set)
	lastEquippedSet = set
	updateDisplay()
	menuNeedsUpdate = true
end
local function removeLatest(event, set)
	menuNeedsUpdate = true
	if lastEquippedSet == set then
		lastEquippedSet = nil
		updateDisplay()
	end
end
local function init()
	for k in pairs(ClosetGnome.db.char.set) do
		if ClosetGnome:IsSetFullyEquipped(k) then
			updateLatest(nil, k)
			break
		end
	end
	if icon then
		icon:Register("ClosetGnome", CGLDB, ClosetGnome.db.profile.minimap)
	end
end

ClosetGnome.RegisterCallback(CGLDB, "UpdateSet", updateLatest)
ClosetGnome.RegisterCallback(CGLDB, "AddSet", updateLatest)
ClosetGnome.RegisterCallback(CGLDB, "WearSet", updateLatest)
ClosetGnome.RegisterCallback(CGLDB, "DeleteSet", removeLatest)
ClosetGnome.RegisterCallback(CGLDB, "Initialized", init)

